package com.joymart.wechat;


import com.joymart.common.utils.SecurityUtils;
import com.joymart.user.service.UserService;
import com.joymart.wechat.service.WechatOpenPlatformService;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class WechatController {

    @Autowired
    private UserService userService;

    @Autowired
    private WechatOpenPlatformService wechatOpenPlatformService;


    @SneakyThrows
    @PutMapping("/openId/mine")
    public void bindOpenId(@RequestParam String code) {
        String userId = SecurityUtils.getCurrentUserId();
        userService.bindOpenId(userId, code);
    }



    @PostMapping("/qrcode/requests")
    public ResponseEntity<byte[]> generateWXAUlimitCode(@RequestBody GenCodeRequestParam param) {
        byte[] body = wechatOpenPlatformService.getWXAcodeUnlimit(
                param.getEnvVersion(),
                param.getPage(),
                param.getWidth(),
                param.getScene()
        );
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setCacheControl("no-cache, no-store, must-revalidate");
        return new ResponseEntity<>(body, headers, HttpStatus.OK);
    }


    @Data
    private static class GenCodeRequestParam{
        private String envVersion;
        private String page;
        private int width;
        private String scene;
    }


}
